<template>
  <div class="demo">
    <div class="demo-title">只上传 png 图片</div>
    <div class="demo-content">
      <Upload
        v-model:file-list="files"
        action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
        :before-upload="beforeUpload"
        @change="handleChange"
      >
        <Button><UploadOutlined />Upload png only</Button>
      </Upload>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { UploadOutlined } from '@ant-design/icons-vue';
  import Message from '@sscd/message';
  import Button from '@sscd/button';
  import Upload from '@sscd/upload';
  import type { UploadChangeParam, UploadProps } from '@sscd/upload';
  const files = ref<UploadProps['fileList']>([
    {
      uid: '1',
      name: 'xxx.png',
      status: 'done',
      response: 'Server Error 500', // custom error message to show
      url: 'http://www.baidu.com/xxx.png',
    },
    {
      uid: '2',
      name: 'yyy.png',
      status: 'done',
      url: 'http://www.baidu.com/yyy.png',
    },
    {
      uid: '3',
      name: 'zzz.png',
      status: 'error',
      response: 'Server Error 500', // custom error message to show
      url: 'http://www.baidu.com/zzz.png',
    },
  ]);
  const handleChange = ({ file, fileList }: UploadChangeParam) => {
    if (file.status !== 'uploading') {
      console.log(file, fileList);
    }
  };
  const beforeUpload: UploadProps['beforeUpload'] = (file) => {
    const isPNG = file.type === 'image/png';
    if (!isPNG) {
      Message.error(`${file.name} is not a png file`);
    }
    return isPNG || Upload.LIST_IGNORE;
  };
</script>
